package cn.com.gt3.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.common.exception.BusinessException;
import cn.com.common.exception.code.BaseResponseCode;
import cn.com.gt3.entity.DmGySwjgEntity;
import cn.com.gt3.mapper.DmGySwjgMapper;
import cn.com.gt3.service.DmGySwjgService;
import cn.com.system.service.impl.HttpSessionService;
import cn.com.system.vo.res.DeptRespNodeVO;
import cn.com.util.TsBqUtils;

@Service
public class DmGySwjgServiceImpl extends ServiceImpl<DmGySwjgMapper, DmGySwjgEntity> implements DmGySwjgService {

	@Resource
	private HttpSessionService httpSessionService;

	@Resource
	private DmGySwjgMapper dmGySwjgMapper;

	@Override
	public List<DeptRespNodeVO> deptTreeList(String deptId) {
		String currentDeptId = httpSessionService.getCurrentDeptId();
		List<DmGySwjgEntity> list = new ArrayList<>();

		DmGySwjgEntity dmGySwjgEntity = this.getById(currentDeptId);

		String sjswjgdm = dmGySwjgEntity.getSjswjgDm();
		List<DmGySwjgEntity> dmGySwjgEntityList = TsBqUtils.recursionLevelTaxOrg(sjswjgdm);
		List<String> taxOrglist = new ArrayList<String>();
		taxOrglist.add(sjswjgdm);
		if (!TsBqUtils.isNull(dmGySwjgEntityList)) {
			for (DmGySwjgEntity dmGySwjg : dmGySwjgEntityList) {
				String swjgDm = dmGySwjg.getSwjgDm();
				taxOrglist.add(swjgDm);
			}
		}

		LambdaQueryWrapper<DmGySwjgEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(DmGySwjgEntity::getYxbz, "Y");
		queryWrapper.in(DmGySwjgEntity::getSwjgDm, taxOrglist);

		queryWrapper.orderByAsc(DmGySwjgEntity::getSwjgDm);

		if (TsBqUtils.isEmpty(deptId)) {

			list = dmGySwjgMapper.selectList(queryWrapper);
			for (DmGySwjgEntity swjgEntity : list) {
				if (swjgEntity.getSwjgDm().equals(sjswjgdm)) {
					swjgEntity.setSjswjgDm("00000000000");
				}
			}

		} else {

			DmGySwjgEntity sjDmGySwjgEntity = this.getById(sjswjgdm);
			if (sjDmGySwjgEntity == null) {
				throw new BusinessException(BaseResponseCode.DATA_ERROR);
			}

			queryWrapper.likeRight(DmGySwjgEntity::getSwjgDm, deptId);

			List<Object> childIds = dmGySwjgMapper.selectObjs(queryWrapper);
			list = dmGySwjgMapper
					.selectList(Wrappers.<DmGySwjgEntity>lambdaQuery().notIn(DmGySwjgEntity::getSwjgDm, childIds));

		}

		// 默认加一个顶级方便新增顶级部门
		DeptRespNodeVO respNodeVO = new DeptRespNodeVO();
		respNodeVO.setTitle("默认顶级部门");
		respNodeVO.setId("00000000000");
		respNodeVO.setSpread(true);
		respNodeVO.setDisabled(true);
		respNodeVO.setChildren(getTree(list));
		List<DeptRespNodeVO> result = new ArrayList<>();
		result.add(respNodeVO);
		return result;

	}

	private List<DeptRespNodeVO> getTree(List<DmGySwjgEntity> all) {
		List<DeptRespNodeVO> list = new ArrayList<>();
		for (DmGySwjgEntity sysDept : all) {
			if ("00000000000".equals(sysDept.getSjswjgDm())) {
				DeptRespNodeVO deptTree = new DeptRespNodeVO();

				deptTree.setId(sysDept.getSwjgDm());
				deptTree.setDeptNo(sysDept.getSwjgDm());
				deptTree.setPid(sysDept.getSjswjgDm());
				deptTree.setTitle(sysDept.getSwjgmc());
				deptTree.setSpread(true);
				deptTree.setChildren(getChild(sysDept.getSwjgDm(), all));
				list.add(deptTree);
			}
		}
		return list;
	}

	private List<DeptRespNodeVO> getChild(String id, List<DmGySwjgEntity> all) {
		List<DeptRespNodeVO> list = new ArrayList<>();
		for (DmGySwjgEntity sysDept : all) {
			if (sysDept.getSjswjgDm().equals(id)) {
				DeptRespNodeVO deptTree = new DeptRespNodeVO();
				deptTree.setId(sysDept.getSwjgDm());
				deptTree.setDeptNo(sysDept.getSwjgDm());
				deptTree.setPid(sysDept.getSjswjgDm());
				deptTree.setTitle(sysDept.getSwjgmc());
				deptTree.setChildren(getChild(sysDept.getSwjgDm(), all));
				list.add(deptTree);
			}
		}
		return list;
	}

	@Override
	public String getSjswjgDm(String currentDeptId) {
		return this.getById(currentDeptId).getSjswjgDm();
	}
	
	@Override
	public DmGySwjgEntity getSwjgDm(String taxcode) {
		return this.getById(taxcode);
	}
	
	@Override
	public List<DmGySwjgEntity> selectBycurrentDeptId(String currentDeptId) {
		LambdaQueryWrapper<DmGySwjgEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(DmGySwjgEntity::getYxbz, "Y");
		queryWrapper.like(DmGySwjgEntity::getSwjgDm, currentDeptId.substring(0,9));
		queryWrapper.eq(DmGySwjgEntity::getSwjgbz, "1");
		queryWrapper.orderByDesc(DmGySwjgEntity::getSwjgDm);
		
		return this.list(queryWrapper);
	}

	@Override
	public List<DmGySwjgEntity> getDmGyswjgByCurrentMan() {
		
		String currentDeptId = httpSessionService.getCurrentDeptId();
		String substring = currentDeptId.substring(0, 9);
		LambdaQueryWrapper<DmGySwjgEntity> queryWrapper = Wrappers.lambdaQuery();
		
		queryWrapper.eq(DmGySwjgEntity::getYxbz, "Y");
		queryWrapper.like(DmGySwjgEntity::getSwjgmc, "%沈阳市%稽查%");
		queryWrapper.like(DmGySwjgEntity::getSjswjgDm, substring);
		
		List<DmGySwjgEntity> list = this.list(queryWrapper);
		
		return list;
	}

	@Override
	public List<DmGySwjgEntity> getSwjgByJcAndDept(String substring) {
		
		
		return null;
	}

}
